function connect(){
    var socket = new WebSocket( "ws://localhost:3000/websocket" );

    socket.onopen = function(){
        console.log(1);
    };
    socket.onclose = function(){
        console.log(2);
    };
    socket.onmessage = function(){
        console.log(3);
    };

    return socket;
}
var socket = connect();
bindEvent();

function bindEvent(){
    var $demo = $( "#demo" );
    $demo.on( "blur", function(){
        var val = $( this ).val();
        if( !val ) return;
        if (socket.readyState != WebSocket.OPEN) return;

        socket.send( val );
    } );
}